<?php

namespace lite\libs;

use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\HttpException;
use lite\models\Weixin;

class CommonController extends Controller {

    /**
     * 检查用户权限并返回
     *
     * @author tony@misihui.com
     * @since 2015-01-13
     *
     * @param $permit 权限项名称，$isAjax是否ajax，决定返回内容
     * @return 直接向浏览器返回结果
     */
    protected function checkAccessAndResponse($permit, $isAjax = true) {
        if(Yii::$app->user->can($permit)) {
            return true;
        } else {
            if($isAjax) {
                echo json_encode(['status' => -1, 'code' => 0, 'message' => 'ACCESS DENIED!']);
                exit();
            } else {
                Yii::$app->user->returnUrl = Yii::$app->request->url;
                $this->redirect('/login', true)->send();
                //echo 'ACCESS DENIED!';
                exit();
            }
        }
    }

    //ajax返回，默认status为1成功
    protected function ajaxResponse($data, $status) {
        //@formatter:off
        return Yii::createObject([
            'class' => 'yii\web\Response',
            'format' => \yii\web\Response::FORMAT_JSON,
            'data' => [
                'status' => $status,
                'data' => $data,
            ],
        ]);
        //@formatter:on
    }

    //ajax返回，失败
    protected function ajaxResponseError($errMsg, $errCode = 0) {
        //@formatter:off
        return Yii::createObject([
            'class' => 'yii\web\Response',
            'format' => \yii\web\Response::FORMAT_JSON,
            'data' => [
                'status' => -1,
                'code' => $errCode,
                'message' => $errMsg,
            ],
        ]);
        //@formatter:on
    }

    protected function ajaxResponseNoStatus($data) {
        //@formatter:off
        return Yii::createObject([
            'class' => 'yii\web\Response',
            'format' => \yii\web\Response::FORMAT_JSON,
            'data' => $data,
        ]);
        //@formatter:on
    }

    //ajax返回，成功
    protected function ajaxResponseSuccess($data = []) {
        return $this->ajaxResponse($data, 1);
    }

    //检查是否Ajax Request，不是则返回
    protected function checkIsAjaxRequestAndResponse() {
        if(!Yii::$app->request->isAjax) {
            echo "ACCESS DENIED! Only accept AJAX request";
            exit();
        } else {
            return true;
        }
    }



    /**
     * 从约定数组中得到主数据(data)
     *
     * @author tony@misihui.com
     * @since 2015-01-09
     *
     * @param 输入的ajax数据形如
     *      [
     *        type => xxx,
     *        data => [
     *          d1 => [...],
     *          d2 => [...],
     *          ...
     *       ]
     *     ]
     * @return $itemNames = ''：返回整个data内容
     *                    是数组：返回数组中指定key的data内容
     *                    是字符：返回该变量指定key的data内容
     *         如果找不到该数组，则返回空数组
     */
    protected function getAjaxData($itemNames = '') {
        $data = Yii::$app->request->post('data');

        //返回全部数据
        if(empty($itemNames)) {
            return $data;

            //返回指定的数据
        } else if(is_array($itemNames)) {

            $returnArray = [];
            foreach($itemNames as $item) {
                $returnArray[$item] = isset($data[$item]) ? $data[$item] : [];
            }

            return $returnArray;
        } else {
            return isset($data[$itemNames]) ? $data[$itemNames] : [];
        }
    }

    //从约定数组中得到Type
    protected function getAjaxType() {
        $type = Yii::$app->request->post('type');
        return $type;
    }

    protected function checkIsLogin(){
        $access_token = Yii::$app->request->post("access_token");
        if(empty($access_token)) {
            die(json_encode(["status"=>-99,"message"=>"请先登录"],true));
        }
        $user = Weixin::checkIsLogin($access_token);
        if(!$user){
            die(json_encode(["status"=>-99,"message"=>"请先登录"],true));
        }
        return $user;
    }

}
